#!/usr/bin/env python3
"""
MinIO CLI 使用示例

展示如何在 Python 项目中使用 MinIO CLI 客户端和缓存管理器。
"""

from minio_cli import MinIOClient, CacheManager
from pathlib import Path


def example_basic_operations():
    """基本操作示例"""
    print("="*60)
    print("  示例 1: 基本文件操作")
    print("="*60)
    
    # 创建客户端
    client = MinIOClient()
    
    # 初始化存储桶
    print("\n初始化存储桶...")
    client.init_buckets()
    
    # 上传文件
    print("\n上传文件示例...")
    # client.upload_file(
    #     bucket_type='datasets',
    #     project_name='my-project',
    #     local_file_path='/path/to/your/file.mp4'
    # )
    
    # 下载文件
    print("\n下载文件示例...")
    # client.download_file(
    #     bucket_type='models',
    #     project_name='my-project',
    #     remote_filename='best.pt',
    #     local_file_path='./models/best.pt'
    # )
    
    # 列出文件
    print("\n列出文件示例...")
    # objects = client.list_objects(
    #     bucket_type='datasets',
    #     project_name='my-project'
    # )
    # for obj in objects:
    #     print(f"  {obj['name']}: {obj['size']} bytes")
    
    print("\n✓ 基本操作示例完成")


def example_batch_operations():
    """批量操作示例（目录上传/下载）"""
    print("\n" + "="*60)
    print("  示例 2: 批量操作（目录上传/下载）")
    print("="*60)
    
    client = MinIOClient()
    
    # 批量上传目录
    print("\n批量上传目录示例...")
    # result = client.upload_directory(
    #     bucket_type='datasets',
    #     project_name='my-project',
    #     local_dir_path='./data/',
    #     recursive=True
    # )
    # print(f"上传结果: 成功 {result['success']}, 失败 {result['failed']}")
    
    # 批量上传，只上传特定类型文件
    print("\n批量上传特定类型文件示例...")
    # result = client.upload_directory(
    #     bucket_type='datasets',
    #     project_name='my-project',
    #     local_dir_path='./videos/',
    #     pattern='*.mp4',  # 只上传 .mp4 文件
    #     recursive=True
    # )
    # print(f"上传 .mp4 文件: {result['success']} 个")
    
    # 批量上传到远程子目录
    print("\n批量上传到远程子目录示例...")
    # result = client.upload_directory(
    #     bucket_type='models',
    #     project_name='my-project',
    #     local_dir_path='./checkpoints/',
    #     remote_dir_prefix='v1.0',  # 上传到 v1.0/ 目录下
    #     recursive=True
    # )
    
    # 批量下载
    print("\n批量下载示例...")
    # result = client.download_directory(
    #     bucket_type='datasets',
    #     project_name='my-project',
    #     local_dir_path='./downloaded_data/'
    # )
    # print(f"下载结果: 成功 {result['success']}, 失败 {result['failed']}")
    
    # 批量下载特定类型文件
    print("\n批量下载特定类型文件示例...")
    # result = client.download_directory(
    #     bucket_type='models',
    #     project_name='my-project',
    #     local_dir_path='./models/',
    #     pattern='*.pt'  # 只下载 .pt 文件
    # )
    
    # 批量下载指定前缀下的文件
    print("\n批量下载指定前缀下的文件示例...")
    # result = client.download_directory(
    #     bucket_type='models',
    #     project_name='my-project',
    #     local_dir_path='./models_v1/',
    #     remote_dir_prefix='v1.0',
    #     pattern='*.pt'
    # )
    
    print("\n✓ 批量操作示例完成")


def example_cache_operations():
    """缓存操作示例"""
    print("\n" + "="*60)
    print("  示例 3: 缓存管理")
    print("="*60)
    
    # 创建缓存管理器
    cache = CacheManager()
    
    # 从 MinIO 下载到本地缓存
    print("\n下载到缓存示例...")
    # cache.upload_to_cache(
    #     bucket_type='datasets',
    #     project_name='my-project',
    #     remote_filename='video.mp4'
    # )
    
    # 从本地缓存上传到 MinIO
    print("\n从缓存上传示例...")
    # cache.upload_from_cache(
    #     bucket_type='models',
    #     project_name='my-project',
    #     filename='best.pt'
    # )
    
    # 同步整个项目到缓存
    print("\n同步项目到缓存示例...")
    # cache.sync_project_to_cache(
    #     bucket_type='datasets',
    #     project_name='my-project',
    #     overwrite=False
    # )
    
    # 获取缓存大小
    print("\n获取缓存大小示例...")
    # size = cache.get_cache_size(bucket_type='datasets')
    # print(f"缓存大小: {size / (1024**2):.2f} MB")
    
    print("\n✓ 缓存管理示例完成")


def example_training_workflow():
    """训练工作流示例"""
    print("\n" + "="*60)
    print("  示例 4: 训练工作流")
    print("="*60)
    
    client = MinIOClient()
    cache = CacheManager()
    
    project_name = "yolo-project"
    
    # 1. 下载训练数据
    print("\n步骤 1: 下载训练数据到本地缓存...")
    # cache.sync_project_to_cache(
    #     bucket_type='datasets',
    #     project_name=project_name
    # )
    
    # 2. 训练模型
    print("\n步骤 2: 训练模型...")
    # train_model()  # 你的训练代码
    
    # 3. 上传训练结果
    print("\n步骤 3: 上传模型文件...")
    # client.upload_file(
    #     bucket_type='models',
    #     project_name=project_name,
    #     local_file_path='./best.pt',
    #     remote_filename='best_v1.0.pt'
    # )
    
    # 4. 上传训练日志
    print("\n步骤 4: 上传训练日志...")
    # client.upload_file(
    #     bucket_type='results',
    #     project_name=project_name,
    #     local_file_path='./train.log',
    #     remote_filename='train_log_v1.0.txt'
    # )
    
    print("\n✓ 训练工作流示例完成")


def example_inference_workflow():
    """推理工作流示例"""
    print("\n" + "="*60)
    print("  示例 5: 推理工作流")
    print("="*60)
    
    client = MinIOClient()
    cache = CacheManager()
    
    project_name = "yolo-project"
    
    # 1. 下载模型
    print("\n步骤 1: 下载模型...")
    # cache.upload_to_cache(
    #     bucket_type='models',
    #     project_name=project_name,
    #     remote_filename='best_v1.0.pt'
    # )
    
    # 2. 下载测试数据
    print("\n步骤 2: 下载测试数据...")
    # cache.upload_to_cache(
    #     bucket_type='datasets',
    #     project_name=project_name,
    #     remote_filename='test_video.mp4'
    # )
    
    # 3. 运行推理
    print("\n步骤 3: 运行推理...")
    # run_inference()  # 你的推理代码
    
    # 4. 上传推理结果
    print("\n步骤 4: 上传推理结果...")
    # client.upload_file(
    #     bucket_type='results',
    #     project_name=project_name,
    #     local_file_path='./output.json',
    #     remote_filename='inference_result.json'
    # )
    
    print("\n✓ 推理工作流示例完成")


def example_jenkins_simulation():
    """Jenkins 流水线模拟示例"""
    print("\n" + "="*60)
    print("  示例 6: Jenkins 流水线模拟")
    print("="*60)
    
    import os
    
    # 模拟 Jenkins 环境变量
    print("\n设置环境变量（Jenkins 中会自动设置）...")
    # os.environ['MINIO_ENDPOINT'] = 'minio.internal.com:9000'
    # os.environ['MINIO_ACCESS_KEY'] = 'jenkins_key'
    # os.environ['MINIO_SECRET_KEY'] = 'jenkins_secret'
    # os.environ['ENV'] = 'prod'
    
    # 客户端会自动读取环境变量
    client = MinIOClient()
    print(f"当前环境: {client.environment}")
    print(f"MinIO 端点: {client.config['minio']['endpoint']}")
    
    # 执行流水线任务...
    # 下载数据 -> 训练/测试 -> 上传结果
    
    print("\n✓ Jenkins 流水线模拟完成")


def main():
    """主函数"""
    print("\n" + "="*60)
    print("  MinIO CLI 使用示例")
    print("="*60)
    print("\n提示: 代码中的示例操作已注释，取消注释即可使用")
    
    # 运行示例
    example_basic_operations()
    example_batch_operations()
    example_cache_operations()
    example_training_workflow()
    example_inference_workflow()
    example_jenkins_simulation()
    
    print("\n" + "="*60)
    print("  所有示例完成")
    print("="*60)
    print("\n更多信息请参考 README.md")
    print()


if __name__ == '__main__':
    main()

